package com.sunday.fangdaifu.activity.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.qrcode.CaptureActivity;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.base.MyApplication;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 刘涛 on 2017/7/7.
 */

public class UnFinishTaskActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface{

    @Bind(R.id.slide_tab)
    SlidingTabLayout slideTab;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unfinish_task);
        ButterKnife.bind(this);
        userId = MyApplication.getInstance().getMember().getId();
        initTaskViewPager();
    }


    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private void initTaskViewPager() {
        titles.add("待执行");
        titles.add("已过期");
        fragments.add(UnFinishTaskListFragment.newInstance(2));
        fragments.add(UnFinishTaskListFragment.newInstance(3));
        viewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(), fragments,
                titles));
        slideTab.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);

    }

    @OnClick(R.id.floating_button)
    void scan() {

        Intent intent = new Intent(mContext, CaptureActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        Gson gson = new Gson();
        String result = data.getStringExtra("result");
        if (!TextUtils.isEmpty(result) && result.length() > 4) {
            String id = result. substring(result.lastIndexOf('/') + 1,result.length());
            ApiClient.getApiService().taskId(userId, Integer.valueOf(id), this, new TypeToken<ResultDO<Integer>>() {
            }.getType());
        }

    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        ResultDO<Integer> resultDO2 = (ResultDO<Integer>) result;
        if (resultDO2.getCode() == 0) {
            Intent intent = new Intent(mContext, TaskDetailActivity.class);
            intent.putExtra("id", resultDO2.getResult());
            startActivity(intent);
        } else {
            ToastUtils.showToast(mContext, resultDO2.getMessage());
        }
    }

    @Override
    public void onFailure() {
        ToastUtils.showToast(mContext, "网络连接错误");
    }
}
